package staticClass;

import myapp.DAO.DataProvider;
import myapp.DTO.UserDTO;
import myapp.EnumAndConstans.Constants;
import myapp.helper.OrientationHelper;
import myapp.helper.Utils;
import myapp.services.ChatServiceHelper;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Packet;

import android.os.Handler;
import android.util.Log;

public class XmppConnection {
	public static XMPPConnection connection;
	public static String userEmail;
	public static String password;
	public static String hashPwd;
	public static UserDTO recipient;
	private static ChatServiceHelper chatHelper = new ChatServiceHelper();
	private static Handler mHandler = new Handler();
	private static Runnable updateRunable;
	public static Boolean isAutoUpdatingYourLocation = false; 
	
	public XmppConnection(){
		Log.i("XmppConnection", "[Constructor] XXXXXXXXX");
	}
	
	public static void setUserEmailAndPassword(String email, String pwd){
		userEmail = email;
		password = pwd;
		HashPassword();
	}
	
	public static void SetConnection(XMPPConnection connection){
		XmppConnection.connection = connection;
	}
	
	public static void setPacketListener(PacketListener listener, PacketFilter filter){
		if (connection != null){
			connection.addPacketListener(listener, filter);
		}
	}
	
	public static void sendPacket(Packet packet){
		XmppConnection.connection.sendPacket(packet);
	}
	
	public static void LogOutAndCloseConnection(){
		if (connection != null){
			Log.i("XmppConnection", "[LogOutAndCloseConnection] disconnect");
			updateAvailable(Constants.NOT_AVAILABLE);
			connection.disconnect();
			connection = null;
		}
	}
	
	public static boolean isConnected(){
		return (connection != null);
	}
	
	public static void updateUserLocation(){
		if (!isConnected()){
			Log.e("XmppConnection", "[updateUserLocation] notConected");
			return;
		}
		UserDTO info = new UserDTO(userEmail, OrientationHelper.getCurLat(), OrientationHelper.getCurLon());
		chatHelper.UpdateUser(info);
	}
	
	private static void updatePositionToDefault(){
		UserDTO info = new UserDTO(userEmail, Constants.DEFAULT_LAT, Constants.DEFAULT_LONG);
		chatHelper.UpdateUser(info);
	}
	
	public static void startAutoUpdating(){
		updateRunable = new Runnable() {			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				UserDTO info = new UserDTO(userEmail, OrientationHelper.getCurLat(), OrientationHelper.getCurLon());
				chatHelper.UpdateUser(info);
				
				mHandler.removeCallbacks(updateRunable);
				mHandler.postDelayed(updateRunable, Constants.UPDATING_TIME);
			}
		};
		isAutoUpdatingYourLocation = true;
		
		mHandler.postDelayed(updateRunable, Constants.UPDATING_TIME);
		Log.i("XmppConnection", "[startAutoUpdating] started");
	}
	
	public static void updateAvailable(int available){
		UserDTO info = new UserDTO(userEmail, available);
		Log.i("XmppConnection", "[updateAvailable] baseHost:" + DataProvider.dbBaseHost);
		chatHelper.UpdateUser(info);
	}
	
	public static void stopAutoUpdating(){
		try {
			mHandler.removeCallbacks(updateRunable);
			updatePositionToDefault();
			isAutoUpdatingYourLocation = false;
			Log.i("XmppConnection", "[stopAutoUpdating] stopped");
		} catch (Exception e) {
			// TODO: handle exception
			Log.e("XmppConnection", "[stopAutoUpdating] ex:" + e.getLocalizedMessage() + " " + e.getMessage());
		}
	}
	
	public static void ProcessOnDestroyOrStop(){
		Log.i("XmppConnection", "[ProcessOnDestroyOrStop]");
		updateAvailable(Constants.NOT_AVAILABLE);
	}
	
	private static void HashPassword(){
		try {
			hashPwd = Utils.SHA1(password);
			Log.i("XmppConnection", "[SetConnection] hashed:" + hashPwd);
		} catch (Exception e) {
			// TODO: handle exception
			Log.e("XmppConnection", "[SetConnection] passWord hashed rs:" + e.getMessage() + e.getLocalizedMessage());
		}
	}
}
